package cn.iocoder.yudao.module.crm.controller.admin.cluetransconfig;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.crm.controller.admin.cluetransconfig.vo.*;
import cn.iocoder.yudao.module.crm.dal.dataobject.cluetransconfig.CrmClueTransConfigDO;
import cn.iocoder.yudao.module.crm.service.cluetransconfig.CrmClueTransConfigService;

@Tag(name = "管理后台 - 线索转入配置")
@RestController
@RequestMapping("/crm/clue-trans-config")
@Validated
public class CrmClueTransConfigController {

    @Resource
    private CrmClueTransConfigService clueTransConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建线索转入配置")
    public CommonResult<Long> createClueTransConfig(@Valid @RequestBody CrmClueTransConfigSaveReqVO createReqVO) {
        return success(clueTransConfigService.createClueTransConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新线索转入配置")
    public CommonResult<Boolean> updateClueTransConfig(@Valid @RequestBody CrmClueTransConfigSaveReqVO updateReqVO) {
        clueTransConfigService.updateClueTransConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除线索转入配置")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteClueTransConfig(@RequestParam("id") Long id) {
        clueTransConfigService.deleteClueTransConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得线索转入配置")
    @Parameter(name = "moduleId", description = "编号", required = true, example = "1024")
    public CommonResult<CrmClueTransConfigRespVO> getClueTransConfig(@RequestParam("moduleId") Long moduleId) {
        CrmClueTransConfigDO clueTransConfig = clueTransConfigService.getClueTransConfig(moduleId);
        return success(BeanUtils.toBean(clueTransConfig, CrmClueTransConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得线索转入配置分页")
    public CommonResult<PageResult<CrmClueTransConfigRespVO>> getClueTransConfigPage(@Valid CrmClueTransConfigPageReqVO pageReqVO) {
        PageResult<CrmClueTransConfigDO> pageResult = clueTransConfigService.getClueTransConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CrmClueTransConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出线索转入配置 Excel")
    @OperateLog(type = EXPORT)
    public void exportClueTransConfigExcel(@Valid CrmClueTransConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CrmClueTransConfigDO> list = clueTransConfigService.getClueTransConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "线索转入配置.xls", "数据", CrmClueTransConfigRespVO.class,
                        BeanUtils.toBean(list, CrmClueTransConfigRespVO.class));
    }

}